package com.example.xwweibo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xw.common.entity.Comment;
import com.example.xw.common.service.CommentService;
import com.example.xw.common.vo.Result;
import com.example.xwweibo.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController("myCommentServiceImpl")
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    @Transactional
    public Result<Comment> addComment(int wbid, String content, String userloginname) {
        Comment comment = new Comment();
        comment.setWeiboId(wbid);
        comment.setContent(content);
        comment.setUserLoginname(userloginname);
        comment.setState(1);
        comment.setCreateTime(new Date());
        commentMapper.insert(comment);
        return new Result<>(4000,comment,"添加成功");
    }

    @Override
    public Result<Page<Comment>> showListByWeibo(int wbid, int pageNum, int pageSize, int state) {
        Page<Comment> page=new Page<Comment>(pageNum,pageSize);
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<Comment>();
        queryWrapper.lambda().eq(Comment::getWeiboId,wbid).eq(Comment::getState,state);
        commentMapper.selectPage(page,queryWrapper);
        return new Result<>(4000,page,"查询成功");
    }

    @Override
    public Result<Page<Comment>> showListByUser(String userloginname, int pageNum, int pageSize, int state) {
        Page<Comment> page=new Page<Comment>(pageNum,pageSize);
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<Comment>();
        queryWrapper.lambda().eq(Comment::getUserLoginname,userloginname).eq(Comment::getState,state);
        commentMapper.selectPage(page,queryWrapper);
        return new Result<>(4000,page,"查询成功");
    }

    @Override
    @Transactional
    public Result<Integer> removeComment(int cmid) {
        int n = commentMapper.deleteById(cmid);
        return new Result<>(4000,null,"删除成功");
    }
}
